package com.emokili.ewa.core.frame.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.emokili.ewa.core.frame.service.OssService;
import com.emokili.ewa.core.frame.vo.OssVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OssServiceImpl implements OssService {

    @Value("${ACCESS_KEY_ID}")
    private String accessKeyId;
    @Value("${ACCESS_KEY_SECRET}")
    private String accessKeySecret;
    @Value("${ROLE_ARN}")
    private String roleArn;
    @Value("${TOKEN_EXPIRE_TIME}")
    private Long tokenExpireTime;
    /**
     * 目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值
     **/
    public static final String REGION_CN_HANGZHOU = "cn-hangzhou";
    public static final String STS_API_VERSION = "2015-04-01";

    @Override
    public OssVo getOssParams(String serialNumber, String playlistId) {
        //构建一个阿里云客户端，用于发起请求。
        //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tCdqxJBvF9c93psktaA", "LuDetZgKLU0M5OfQja8FiOsjPknaHF");
        IAcsClient client = new DefaultAcsClient(profile);

        //构造请求，设置参数。关于参数含义和设置方法，请参见API参考。
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRegionId("cn-hangzhou");
        // request.setLocationProduct("1/2");
        request.setRoleArn("acs:ram::1006960137574003:role/aliyunosstokengeneratorrole");
        request.setRoleSessionName("external-username");
        String bucketName = "ew10a-dev-upload";
        String ossPath = "1";
        String policy = "{\n" +
                "    \"Statement\": [\n" +
                "      {\n" +
                "        \"Action\": [\n" +
                "          \"oss:GetObject\",\n" +
                "          \"oss:PutObject\",\n" +
                "          \"oss:ListObjects\"\n" +
                "        ],\n" +
                "        \"Effect\": \"Allow\",\n" +
                "        \"Resource\": \"*\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"Version\": \"1\"\n" +
                "}";
        request.setPolicy(policy);
        //发起请求，并得到响应。
        try {
            AssumeRoleResponse response = client.getAcsResponse(request);
            OssVo ossVo = new OssVo();
            ossVo.setAccessKeyId(response.getCredentials().getAccessKeyId());
            ossVo.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
            ossVo.setSecurityToken(response.getCredentials().getSecurityToken());
            ossVo.setExpiration(response.getCredentials().getExpiration());
            ossVo.setStatusCode("200");
            return ossVo;
        } catch (ServerException e) {
            log.error("oss server error",e);
        } catch (ClientException e) {
            log.error("oss client error",e);
        }
        return null;
    }
}
